<?php
/**
 */
defined('BY_JYA') or exit('error');
$op=$_RQ['op']?$_RQ['op']:'get';
$return['ntime']=TIMESTAMP;
if($op=='get'){
	if($_RQ['postdata']){
		
	}
	$pid = intval($_RQ['pid']);
	if(empty($pid)){
		 exi('您没有选择拍品！','error');
	}
	
	$this->model('auction')->checklogin();
	   /** 是否有支付密码 **/
     $Password = pdo_getcolumn('jy_jdpm_member',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid']),'paypwd');
	 if(empty($Password)){
		  confirm('您还没有设置支付密码，请先设置支付密码','jy_jdpm/member.safety/setpay/');
	 }
	$return['post'] =array('pid'=>$pid);
	exi($return);
}
if($op=='checkpay'){
	//支付订单
	$post=get_postdata();
	$id=$post['order_no'];
	if(!$id){
		exi('请先选择订单！','error');
	}
	if(!$post['adid']){
		exi('请先选择收货地址！','error');
	}
	$order=pdo_get('jy_jdpm_order',array('uniacid'=>$_SESSION['uniacid'],'order_no'=>$id));
	if(!$order){
		exi('订单不存在！','error');
	}
	if($order['status']==-1){
		exi('订单已逾期！','error');
	}
	if($order['status']){
		exi('订单已支付！','error');
	}
	$ad=pdo_get('core_address',array('uniacid'=>$_SESSION['uniacid'],'adid'=>$post['adid']));
	$address=array('provaddress'=>$ad['prov'].$ad['city'].$ad['district'],'truename'=>$ad['truename'],'mobile'=>$ad['mobile'],'address'=>$ad['address']);
	$Balance = pdo_get('jy_jdpm_member',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid']),array('wallet_pledge','wallet_pledge_freeze','brokerage'));
	$Balance['balance']=$Balance['wallet_pledge'];
	$Balance['freeze']=$Balance['wallet_pledge_freeze'];
	$UserList=pdo_getall('jy_jdpm_freezelog',array('uniacid'=>$_SESSION['uniacid'],'pid'=>$order['pid'],'uid !='=>$order['mid']),array('mid','createtime'));
	$UserAll=pdo_getall('jy_jdpm_freezelog',array('uniacid'=>$_SESSION['uniacid'],'pid'=>$order['pid'],'mid'=>$order['mid']),array('mid','createtime'));
	$PlayMoney = $order['price'];
	$money=$Balance['balance'] - $Balance['freeze'] - $order['price'] + $Order['margin'];
	if($money<0){
		exi('余额不足！请先充值！','error');
	}
	 //起拍价
        $onset = $order['onset'];
        //平台   2%
        $terrace = $onset * 0.02;
        //参与者 2% 竞拍拥金
        $AllNum = $num = $party = 0;

        if ($UserList) {
            $party  = $onset * 0.03;
            $num    = count($UserList);
            $AllNum = count($UserAll) - 1;
        }

        //手续费
        $Commission = $terrace;
        //平台拥金（包含推广分享金）
        $terrace = ($num + $AllNum) * $terrace;
		$terrace1 =  $terrace;
		$sourceuid=	pdo_getcolumn('jy_jdpm_member',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid']),'sourceuid');
		if($sourceuid){
			$set=$this->plugin_setting('commission');
			$commission=$set['commission']?$set['commission']:5;
			//推广分享金
			$share	 = $terrace*$commission/40;
			//平台最终拥金（不包含推广分享金）
			$terrace1 = $terrace-$share;	
		}
		//首拍送积分
		$l1=pdo_getall('jy_jdpm_freezelog',array('uniacid'=>$_SESSION['uniacid'],'pid'=>$pid),array('mid'),'','time asc');
		if($l1){
			$mid=pdo_getcolumn('jy_jdpm_member',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$l1[0]['mid']),'mid');
			$credit1=pdo_getcolumn('core_members',array('uniacid'=>$_SESSION['uniacid'],'id'=>$mid),'credit1');			
			$add=$set['firstend']?$set['firstend']:2;
			$add=intval($money*$add/100);
			$credit1=$credit1+$add;
			pdo_update('core_members',array('uniacid'=>$_SESSION['uniacid'],'credit1'=>$credit1),array('id'=>$mid));
			pdo_insert('jy_jdpm_credit_log',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$l1[0]['mid'],'is_add'=>1,'type'=>4,'pid'=>$pid,'fee'=>$add,'remark'=>'首拍送积分','createtime'=>time()));
		}
		
		//尾拍积分
		$l=pdo_get('jy_jdpm_freezelog',array('uniacid'=>$_SESSION['uniacid'],'uid <>'=>$_SESSION['uid'],'pid'=>$pid));
		if($l){
			$credit1=pdo_getcolumn('core_members',array('id'=>$_SESSION['uid']),'credit1');
			$set=$this->plugin_setting('basic');
			$add=$set['firstend']?$set['firstend']:2;
			$add=intval($order['price']*$add/100);
			$credit1=$credit1+$add;
			pdo_update('core_members',array('uniacid'=>$_SESSION['uniacid'],'credit1'=>$credit1),array('id'=>$_SESSION['uid']));
			pdo_insert('jy_jdpm_credit_log',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid'],'is_add'=>1,'type'=>4,'pid'=>$pid,'fee'=>$add,'remark'=>'尾拍送积分','createtime'=>time()));
		}
		
        //服务费
        $charge = $order['price'] * 0.01;

        //送拍人
        $income          = $order['price'] - $terrace - ($party * ($num + $AllNum)) - $charge - $Commission;
        $OrderAllocation = $this->plugin_setting('order');
		//更改订单信息
            $OUpData = [
                'status'     => 1 ,
                'buyer_msg'  => $post['buyer_msg'] ,
                'time1'      => time() ,
                'defgetgoodstimest' => 0 ,
                'defgetgoodstime'   => time() + ((24 * 60 * 60) * $OrderAllocation['losetime1']) ,
                'address'    => ijson_encode($address) ,
            ];

            pdo_update('jy_jdpm_order',$OUpData,array('uniacid'=>$_SESSION['uniacid'],'order_no'=>$order['order_no']));


            //更改保证金状态
			pdo_update('jy_jdpm_freezelog',array('status'=>1,'rtime'=>TIMESTAMP),array('uniacid'=>$_SESSION['uniacid'],'pid'=>$order['pid'],'status'=>0));

            if ($Balance['brokerage'] > $PlayMoney) {
                $Brokerage = $Balance['brokerage'] - $PlayMoney;
            }


            //扣除用户金额
            $MUpData = [
                'wallet_pledge'        => $Balance['balance'] - $PlayMoney ,
                'brokerage'            => ($Balance['brokerage'] > $PlayMoney) ? ($Balance['brokerage'] - $PlayMoney) : 0 ,
                'wallet_pledge_freeze' => $Balance['freeze'] - $order['margin'] ,
            ];
			pdo_update('jy_jdpm_member',$MUpData,array('uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid']));
            //卖家收入金额
			pdo_update('jy_jdpm_member',array('wallet_pledge +='=>$income),array('uniacid'=>$_SESSION['uniacid'],'mid'=>$order['merchid']));
            $Log = [
                [   //买家
                    'mid'          => $order['mid'] ,
                    'money'        => $order['price'] ,
                    'channel'      => 2 ,
                    'create_time'  => time() ,
                    'update_time'  => time() ,
                    'auction_time' => 0 ,
                    'remarks'      => '支付拍品订单' ,
                    'type'         => 2 ,
					'uniacid'=>$_SESSION['uniacid'],
                    'pid'          => $order['pid'] ,
                    'role'         => '成交人'
                ] ,
                [   //卖家
                    'mid'          => $order['merchid'] ,
                    'money'        => $income ,
                    'channel'      => 1 ,
                    'create_time'  => time() ,
                    'update_time'  => time() ,
                    'auction_time' => 99999999999 ,
                    'remarks'      => '拍品收入' ,
                    'role'         => '送拍人' ,
                    'type'         => 4 ,
					'uniacid'=>$_SESSION['uniacid'],
                    'pid'          => $order['pid']
                ] ,
                [   //平台
                    'mid'          => 0 ,
                    'money'        => $terrace1 ,
                    'channel'      => 1 ,
                    'create_time'  => time() ,
                    'update_time'  => time() ,
                    'auction_time' => 0 ,
                    'remarks'      => '平台拍品收入' ,
                    'role'         => '平台佣金' ,
                    'type'         => 3 ,
					'uniacid'=>$_SESSION['uniacid'],
                    'pid'          => $order['pid']
                ] ,
                [   //服务费
                    'mid'          => -1 ,
                    'money'        => $charge ,
                    'channel'      => 1 ,
                    'create_time'  => time() ,
                    'update_time'  => time() ,
                    'auction_time' => 2 ,
                    'remarks'      => '服务费收入' ,
                    'role'         => '平台' ,
                    'type'         => 5 ,
					'uniacid'=>$_SESSION['uniacid'],
                    'pid'          => $order['pid']
                ] ,
				
                [   //手续费
                    'mid'          => -2 ,
                    'money'        => $Commission ,
                    'channel'      => 1 ,
                    'create_time'  => time() ,
                    'update_time'  => time() ,
                    'auction_time' => 1 ,
                    'remarks'      => '手续费收入' ,
                    'role'         => '广宝' ,
                    'type'         => 5 ,
					'uniacid'=>$_SESSION['uniacid'],
                    'pid'          => $order['pid']
                ]
            ];
			if($sourceuid){
				$Log[] = [
                        'mid'          => $sourceuid ,
                        'money'        => $share ,
                        'channel'      => 1 ,
                        'create_time'  => time() ,
                        'update_time'  => time() ,
                        'auction_time' => 0 ,
                        'remarks'      => '推广分享金收入' ,
                        'type'         => 7 ,
						'uniacid'=>$_SESSION['uniacid'],
                        'pid'          => $order['pid']
                    ];
				 pdo_update('jy_jdpm_member',array('brokerage +='=>$share,'wallet_pledge +='=>$share),array('uniacid'=>$_SESSION['uniacid'],'mid'=>$sourceuid));
			}
            //发放竞拍拥金
            if (!empty($UserList)) {
                foreach ($UserList AS $value) {
                    pdo_update('jy_jdpm_member',array('brokerage +='=>$party,'wallet_pledge +='=>$party),array('uniacid'=>$_SESSION['uniacid'],'mid'=>$value['mid']));
                    $Log[] = [
                        'mid'          => (int)$value['mid'] ,
                        'money'        => $party ,
                        'channel'      => 1 ,
                        'create_time'  => time() ,
                        'update_time'  => time() ,
                        'auction_time' => (int)$value['createtime'] ,
                        'remarks'      => '拍品分享金收入' ,
                        'type'         => 3 ,
						'uniacid'=>$_SESSION['uniacid'],
                        'pid'          => $order['pid']
                    ];
                }
            }


            if ($AllNum > 0) {
                for ($i = 0; $i < $AllNum; $i++) {
                    pdo_update('jy_jdpm_member',array('wallet_pledge +='=>$party,'brokerage +='=>$party),array('uniacid'=>$_SESSION['uniacid'],'mid'=>$order['mid']));
                    $Log[] = [
                        'mid'          => (int)$UserAll[$i]['mid'] ,
                        'money'        => $party ,
                        'channel'      => 1 ,
                        'create_time'  => time() ,
                        'update_time'  => time() ,
                        'auction_time' => (int)$UserAll[$i]['createtime'] ,
                        'remarks'      => '拍品分享金收入' ,
                        'type'         => 3 ,
						'uniacid'=>$_SESSION['uniacid'],
                        'pid'          => $order['pid']
                    ];
                }
            }

            $Log = $this->array_sort($Log , 'auction_time' , $type = 'asc');
            ksort($Log);
            foreach ($Log AS $value) {
                pdo_insert('jy_jdpm_money_log',$value);
            }
	exi('支付成功！','','member.index/');
}
?>